const GuyueModule = {
	namespaced: true, // 启用命名空间
	state: {
		Tabdisabled: false,
		currentTimeString: '', // 初始时间
		timerId: null, // 用于存储定时器的ID  
		latestMesBasicInfo: {}, //初始化的控制台基础 数据
		MesBasicInfo: {}, // 控制台基础数据
		MesBasicInfoList: [], // 控制台基础数据
		ModuleData: [], // index页面模块数据
		ModuleData_select: [{ // index页面选中层级数据
			Name: '工作台',
			MesMenuID: 0,
			length: 2,
			nood: []
		}],
		moduleData_basic: [{ // index页面选中层级数据
			Name: '工作台',
			MesMenuID: 0,
			length: 2,
			nood: []
		}],
		mesRouteInfo: { // 工作台所处页面栈信息，用于回退使用
			routeIndex: null,
			route: null,
			options: null,
		},
	},
	mutations: {
		/** 修改state 数据_测试
		 * 	payload.Name1 - state层级属性数据 如 user.info.score
		 * 	payload.data  - 赋值内容
		 *  payload.StorageName  - 缓存名称，有缓存时会开启缓存
		 *  payload.StorageData  - 缓存内容，已处理成JSON格式 
		 * 	@param {object} payload  这是一个对象，包含属性名，赋值内容，缓存名、缓存内容
		 */
		ChangeAndSetStateData(state, payload) {
			// 解构出要修改的属性名
			const {
				Name1,
				data,
				StorageName,
				StorageData,
			} = payload;

			// 如果属性名存在多级，可以使用动态属性名来修改对应的值
			const levels = Name1.split('.');
			let currentState = state;
			for (let i = 0; i < levels.length - 1; i++) {
				currentState = currentState[levels[i]];
			}
			currentState[levels[levels.length - 1]] = data;

			// 根据StorageName参数来决定是否记录缓存
			if (StorageName) {
				uni.setStorageSync(StorageName, StorageData);
			}
		},
		UPDATE_MES_BASIC_INFO(state, payload) {
			state.MesBasicInfo = Object.assign({}, state.MesBasicInfo, payload);
		},
		SET_CURRENT_TIME(state, timeString) {
			state.currentTimeString = timeString;
		},
		SET_TIMER_ID(state, timerId) {
			state.timerId = timerId;
		},
		CLEAR_TIMER_ID(state) {
			state.timerId = null;
		},
		UPDATE_MES_INFO(state, payload) {
			state.MesBasicInfo = {
				...state.MesBasicInfo,
				...payload
			}
		},
		UPDATE_MODULE_DATA(state, payload) {
			state.moduleData = payload
		}
	},
	actions: {
		startUpdatingTime({
			commit
		}) {
			const updateTime = () => {
				const now = new Date();
				const year = now.getFullYear();
				const month = String(now.getMonth() + 1).padStart(2, '0');
				const day = String(now.getDate()).padStart(2, '0');
				const hour = String(now.getHours()).padStart(2, '0');
				const minute = String(now.getMinutes()).padStart(2, '0');
				const second = String(now.getSeconds()).padStart(2, '0');
				const timeString = `${year}-${month}-${day} ${hour}:${minute}:${second}`;
				commit('SET_CURRENT_TIME', timeString);
			};

			const timerId = setInterval(updateTime, 1000); // 每秒更新时间  
			commit('SET_TIMER_ID', timerId);
		},
		stopUpdatingTime({
			commit,
			state
		}) {
			if (state.timerId) {
				clearInterval(state.timerId);
				commit('CLEAR_TIMER_ID');
			}
		}
	},
	getters: {}
};

export default GuyueModule;